<template>
  <div class='top-category'>
    <div class="container">
    <!-- 面包屑导航 -->
     <XtxBread>
       <XtxBreadItem to="/">主页</XtxBreadItem>
       <XtxBreadItem>{{cate.name}}</XtxBreadItem>
     </XtxBread>
     <!-- 轮播图 -->
     <XtxCarousel :sliders="sliders" style="height:500px" />
     <!-- 所有二级分类 -->
     <div class="sub-list">
         <h3>全部分类</h3>
         <ul>
           <li v-for="sub in cate.children" :key="sub.id">
             <a href="javascript:;">
               <img :src="sub.picture" >
               <p>{{sub.name}}</p>
             </a>
           </li>
         </ul>
     </div>
     <!-- 分类关联商品 -->
     <div class="ref-goods" v-for="sub in cate.children" :key="sub.id">
       <div class="head">
         <h3>-  {{sub.name}}  -</h3>
         <XtxMore to="/" />
       </div>
       <div class="body">
         <GoodsItem v-for="good in sub.goods" :key="good.id" :good="good" />
       </div>
     </div>
    </div>
  </div>
</template>

<script>
import GoodsItem from './components/goods-item.vue'
import { ref, onMounted } from 'vue'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'
import { findBanner } from '../../api/home'
import { findTopCategory } from '../../api/category'

export default {
  name: 'category',
  components: { GoodsItem },
  setup () {
    // 1.轮播图
    const sliders = ref([])

    // 2.分类数据
    const route = useRoute()
    const cate = ref({})

    const loadData = (id) => {
      // 轮播图
      findBanner().then(data => {
        console.log('findBanner', data)
        sliders.value = data.result
      })
      findTopCategory(id).then(data => {
        // 分类数据
        console.log('findTopCategory', data)
        cate.value = data.result
      })
    }

    // 3.只执行一次
    onMounted(() => {
      loadData(route.params.id)
    })
    // 4.路由跳转时执行
    onBeforeRouteUpdate((to) => {
      console.log('onBeforeRouteUpdate', to)
      loadData(to.params.id)
    })
    return { sliders, cate }
  }
}
</script>

<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: space-around;
      padding: 0 40px 30px;
    }
  }
}
</style>
